package handler

import (
	"context"
	. "rbac/models"
	pb "rbac/proto/rbacAccess"
	"strconv"
)

type RbacAccess struct{}

// 获取权限
func (e *RbacAccess) AccessGet(ctx context.Context, req *pb.AccessGetRequest, res *pb.AccessGetResponse) error {
	var accessList []Access
	where := "1=1"
	if req.Id > 0 {
		where += " AND id=?" + strconv.Itoa(int(req.Id))
	} else {
		where += " AND module_id=0"
	}
	DB.Where(where).Preload("AccessItem").Find(&accessList)
	var tempList []*pb.AccessModel
	for _, v := range accessList {
		var tempItemList []*pb.AccessModel
		for _, t := range v.AccessItem {
			tempItemList = append(tempItemList, &pb.AccessModel{
				ModuleName:  t.ModuleName,
				ActionName:  t.ActionName,
				Type:        int64(t.Type),
				Url:         t.Url,
				ModuleId:    int64(t.ModuleID),
				Sort:        int64(t.Sort),
				Description: t.Description,
				Status:      int64(t.Status),
				AddTime:     int64(t.AddTime),
			})
		}

		tempList = append(tempList, &pb.AccessModel{
			ModuleName:  v.ModuleName,
			ActionName:  v.ActionName,
			Type:        int64(v.Type),
			Url:         v.Url,
			ModuleId:    int64(v.ModuleID),
			Sort:        int64(v.Sort),
			Description: v.Description,
			Status:      int64(v.Status),
			AddTime:     int64(v.AddTime),
			AccessItem:  tempItemList,
		})
	}
	res.AccessList = tempList
	return nil
}

// 增加权限
func (e *RbacAccess) AccessAdd(ctx context.Context, req *pb.AccessAddRequest, res *pb.AccessAddResponse) error {
	access := Access{
		ModuleName:  req.ModuleName,
		ActionName:  req.ActionName,
		Url:         req.Url,
		Type:        int(req.Type),
		ModuleID:    int(req.ModuleId),
		Status:      int(req.Status),
		Sort:        int(req.Sort),
		Description: req.Description,
	}
	if err := DB.Create(&access).Error; err != nil {
		res.Success = false
		res.Message = "增加权限失败"
		return err
	}
	res.Success = true
	res.Message = "增加权限成功"
	return nil
}

// 编辑权限
func (e *RbacAccess) AccessEdit(ctx context.Context, req *pb.AccessEditRequest, res *pb.AccessEditResponse) error {
	var access Access
	DB.Where("id = ?", req.Id).Take(&access)
	if access.ID == 0 {
		res.Success = false
		res.Message = "权限不存在"
		return nil
	}
	access.ModuleName = req.ModuleName
	access.ActionName = req.ActionName
	access.Type = int(req.Type)
	access.Url = req.Url
	access.ModuleID = int(req.ModuleId)
	access.Sort = int(req.Sort)
	access.Description = req.Description
	access.Status = int(req.Status)
	if err := DB.Save(&access).Error; err != nil {
		res.Success = false
		res.Message = "修改权限失败"
		return err
	}
	res.Success = true
	res.Message = "修改权限成功"
	return nil
}

// 删除权限
func (e *RbacAccess) AccessDelete(ctx context.Context, req *pb.AccessDeleteRequest, res *pb.AccessDeleteResponse) error {
	var access Access
	DB.Where("id = ?", req.Id).Take(&access)
	if access.ID == 0 {
		res.Success = false
		res.Message = "权限不存在"
		return nil
	}
	// 如果是顶级权限, 查看是否存在子权限
	if access.ModuleID == 0 {
		var accessList []Access
		DB.Where("module_id = ?", access.ID).Find(&accessList)
		if len(accessList) > 0 {
			res.Success = false
			res.Message = "存在子权限"
			return nil
		}
	}
	if err := DB.Delete(&access).Error; err != nil {
		res.Success = false
		res.Message = "删除权限失败"
		return err
	}
	res.Success = true
	res.Message = "删除权限成功"
	return nil
}
